package Test_2019_3_19.servlet;

import Test_2019_3_19.pojo.Attendance;
import Test_2019_3_19.util.JdbcUtil;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@WebServlet(name = "AddServlet", urlPatterns = {"/atb/add.do"})
public class AddServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 解决中文乱码
        response.setContentType("application/text; charset=utf-8");
        // 日期
        String demoDate = request.getParameter("demoDate");
        // 状态
        String state1 = request.getParameter("state1");
        String state2 = request.getParameter("state2");
        String state3 = request.getParameter("state3");
        String state4 = request.getParameter("state4");
        // 备注
        String note1 = request.getParameter("note1");
        String note2 = request.getParameter("note2");
        String note3 = request.getParameter("note3");
        String note4 = request.getParameter("note4");
        // 构造
        Attendance attendance1 = new Attendance(1, state1, note1, demoDate);
        Attendance attendance2 = new Attendance(2, state2, note2, demoDate);
        Attendance attendance3 = new Attendance(3, state3, note3, demoDate);
        Attendance attendance4 = new Attendance(4, state4, note4, demoDate);
        // 判断是否成功
        JdbcUtil jdbc = new JdbcUtil();
        jdbc.addAtt(attendance1);
        jdbc.addAtt(attendance2);
        jdbc.addAtt(attendance3);
        int rlt = jdbc.addAtt(attendance4);
        // (判断操作是否成功)转化json格式准备
        Map<String, Object> mjs = new HashMap<String, Object>();
        mjs.put("rlt", rlt);
        // 把数据转化为json格式
        String Json = JSON.toJSONString(mjs);
        response.getWriter().write(Json);
    }
}